package com.zycfc.zsf.boot.util.json;

import java.lang.reflect.*;
import java.io.*;
import com.fasterxml.jackson.core.*;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.reflect.*;
import java.util.*;
import com.zycfc.zsf.boot.util.text.*;
import org.slf4j.*;
import com.google.gson.*;
import com.fasterxml.jackson.databind.*;

public class JsonUtils
{
    private static final Logger log;
    private static Gson gson;
    private static Gson gsonTmp;
    private static final ObjectMapper mapper;
    
    private JsonUtils() {
    }
    
    public static JavaType getJavaType(final Type type) {
        return JsonUtils.mapper.getTypeFactory().constructType(type);
    }
    
    public static <T> T readValue(final String json, final JavaType valueType) throws IOException, JsonParseException, JsonMappingException {
        return (T)JsonUtils.mapper.readValue(json, valueType);
    }
    
    public static String jacksonObjectToJson(final Object o) {
        String labelString = null;
        try {
            labelString = JsonUtils.mapper.writeValueAsString(o);
        }
        catch (JsonProcessingException e) {
            JsonUtils.log.error("\u8f6c\u6362\u5f02\u5e38", (Throwable)e);
        }
        return labelString;
    }
    
    public static <T> T jacksonJsonToObject(final String json, final Class<T> valueType) {
        try {
            return (T)JsonUtils.mapper.readValue(json, (Class)valueType);
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static Map<String, Object> jacksonJsonToMap(final String json) throws IOException, JsonParseException, JsonMappingException {
        return (Map<String, Object>)JsonUtils.mapper.readValue(json, (Class)Map.class);
    }
    
    public static String objectToJson(final Object obj) {
        String gsonString = null;
        if (JsonUtils.gson != null) {
            gsonString = JsonUtils.gson.toJson(obj);
        }
        return gsonString;
    }
    
    public static <T> T jsonToObject(final String json, final Class<T> c) {
        T t = null;
        if (JsonUtils.gson != null) {
            t = (T)JsonUtils.gson.fromJson(json, (Class)c);
        }
        return t;
    }
    
    public static <T> T jsonToObject(final String json, final Type type) {
        T t = null;
        if (JsonUtils.gson != null) {
            t = (T)JsonUtils.gson.fromJson(json, type);
        }
        return t;
    }
    
    public static Map<String, Object> jsonToMap(final String json) {
        Map<String, Object> map = null;
        if (JsonUtils.gson != null) {
            map = (Map<String, Object>)JsonUtils.gson.fromJson(json, new TypeToken<TreeMap<String, Object>>() {}.getType());
        }
        return map;
    }
    
    public static Map<String, Object> jsonToMap(final String json, final boolean numberFlag) {
        Map<String, Object> map = null;
        if (JsonUtils.gsonTmp != null) {
            map = (Map<String, Object>)JsonUtils.gsonTmp.fromJson(json, new TypeToken<TreeMap<String, Object>>() {}.getType());
        }
        return map;
    }
    
    public static <T> List<T> jsonToList(final String json, final Class<T> cls) {
        final Gson gson = new Gson();
        final List<T> list = new ArrayList<T>();
        final JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            list.add((T)gson.fromJson(elem, (Class)cls));
        }
        return list;
    }
    
    public static <T> Set<T> jsonToSet(final String json, final Class<T> cls) {
        final Gson gson = new Gson();
        final Set<T> set = new HashSet<T>();
        final JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            set.add((T)gson.fromJson(elem, (Class)cls));
        }
        return set;
    }
    
    public static <T> List<Map<String, T>> jsonToListMap(final String gsonString) {
        List<Map<String, T>> list = null;
        if (JsonUtils.gson != null) {
            list = (List<Map<String, T>>)JsonUtils.gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {}.getType());
        }
        return list;
    }
    
    public static boolean validate(final String jsonStr) {
        JsonElement jsonElement;
        try {
            jsonElement = new JsonParser().parse(jsonStr);
        }
        catch (Exception e) {
            return false;
        }
        return jsonElement != null && jsonElement.isJsonObject();
    }
    
    public static <T> T jsonToObjectCaseUnderLineToHump(String json, final Class<T> c) {
        final Map<String, Object> jsonMap = jsonToMap(json);
        final Map<String, Object> tempMap = new HashMap<String, Object>();
        final Set<String> keys = jsonMap.keySet();
        final String[] array2;
        final String[] array = array2 = keys.toArray(new String[keys.size()]);
        for (final String key : array2) {
            final Object value = jsonMap.get(key);
            final String humpKey = StringUtils.underlineToCamel(key);
            tempMap.put(humpKey, value);
        }
        json = JsonUtils.gson.toJson((Object)tempMap);
        T t = null;
        if (JsonUtils.gson != null) {
            t = (T)JsonUtils.gson.fromJson(json, (Class)c);
        }
        return t;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)JsonUtils.class);
        JsonUtils.gson = null;
        JsonUtils.gsonTmp = null;
        if (JsonUtils.gson == null) {
            JsonUtils.gson = new GsonBuilder().registerTypeAdapterFactory((TypeAdapterFactory)new NullStringToEmptyAdapterFactory()).registerTypeAdapter((Type)GsonErrCdEnum.class, (Object)new GsonErrCdEnumTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        }
        if (JsonUtils.gsonTmp == null) {
            JsonUtils.gsonTmp = new GsonBuilder().registerTypeAdapterFactory((TypeAdapterFactory)new NullStringToEmptyAdapterFactory()).registerTypeAdapter(new TypeToken<TreeMap<String, Object>>() {}.getType(), (Object)new MapTypeAdapter()).registerTypeAdapter((Type)GsonErrCdEnum.class, (Object)new GsonErrCdEnumTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        }
        (mapper = new ObjectMapper()).configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        JsonUtils.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
